<?php

using('Library.Widgets.Form');
using('Library.Widgets.RecordSet');

class Module_Admin_contentbuilder extends Admin_IModule
{
    var $_Name  = 'contentbuilder';
    var $_Title = 'Content Builder';
    
    /**#@+
     * Required Core Objects
     *
     * These are references to the core objects required by this
     * class.
     *
     * @access private
     * @var    object
     */
    var $_Error;
    var $_Profiler;
    var $_DAL;
    var $_Model;
    /**#@-*/

    function __construct()
    {
        // Get references to required objects
        $this->_Error     = &Vault::Get('Debug_Error');
        $this->_Profiler  = &Vault::Get('Debug_Profiling');
        $this->_DAL       = &Vault::Get('DAL');
        $this->_Model     = &Vault::Get('Model');

        // Register Article Specific Actions
        $this->RegisterAction(array('page'),           'Pages',    'ManagePages_List');
        $this->RegisterAction(array('page', 'create'), 'Create',   'ManagePages_Create');
        $this->RegisterAction(array('page', 'modify'), 'Modify',   'ManagePages_Modify');
        $this->RegisterAction(array('page', 'delete'), 'Delete',   'ManagePages_Delete');
             
        
        // Call Admin_IModule constructor (we call it last so it has access to the actions array)
        parent::__construct();        
    }

    /*
     * Action Methods
     */
    
    function ManagePages_List()
    {
    	/*
    	 * Generate Pages Record Set
    	 */

    	$recordset = new RecordSet($this->GetAction('page'), 'contentbuilder', 'Content Builder [Manage]', 'page');
    	
    	// Recordset Actions
    	$recordset->AddAction('create', 'Create', array(&$this, 'ManagePages_Create'));
    	$recordset->AddAction('delete', 'Delete', array(&$this, 'ManagePages_Delete'));    	
    	
    	// Recordset Fields
    	$recordset->AddField('id', '#', 30);
    	$recordset->AddField('u_display_name', 'Author', 100);    	 	
    	$recordset->AddField('n_title', 'Title', false, array(&$this, '_RecordSet_Page'));
    	
    	// Process Recordset
    	$recordset->Process();        
    }
    
    function ManagePages_Create()
    {
        /*
         * Generate Create News Article Form
         */
        
        $form = new Form('contentbuilder_create', 'http://'.$this->GetAction('page', 'create'));
        
        // Form Actions
        $form->AddAction('save', 'Save', array(&$this, '_CreatePage'), true);        
        $form->AddAction('preview', 'Preview', array(&$this, '_PreviewPage'), false);        
        $form->AddAction('cancel', 'Cancel', array(&$this, 'ManagePages_List'), false);
                
        // Form Fields
        $form->Add(new RichtextField(&$form, 'wysiwyg', 'Content'));
        $form->AddFieldSet('Summary');
        $form->Add(new TextField(&$form, 'title', 'Title', null, null, true));        
        $form->Add(new TextField(&$form, 'author', 'Author', null, $this->_User->DataObject->username, true));
        $form->Add(new DateField(&$form, 'created', 'Created', null, date('Y-m-d'), true));
        $form->Add(new TextareaField(&$form, 'teaser', 'Teaser'));
        $form->AddFieldSet('Meta Data');
        $form->Add(new TextareaField(&$form, 'meta_keywords', 'Keywords'));
        $form->Add(new TextareaField(&$form, 'meta_description', 'Description'));
        $form->Add(new TextField(&$form, 'meta_robots', 'Robots'));
        
        // Process the form
        $form->Process();           
    }
    
    function ManagePages_Modify()
    {
    	/*
    	 * Grab the info of the article to be modified
    	 */
    	
    	if (($page = $this->_DAL->Select('page', $_GET['pid'])) === false)
    	{
    		$this->_Model->Set('Page.Messages.Error', array('Page \''.$_GET['aid'].'\' does not exist.'));
    		$this->ManageArticles_List();
    		return false;
    	}
    	
        /*
         * Generate Modify News Article Form
         */
        
        $form = new Form('contentbuilder_modify', 'http://'.$this->GetAction('page', 'modify').'?pid='.$page->id);
        
        // Form Actions
        $form->AddAction('save', 'Save', array(&$this, '_ModifyPage'), true);        
        $form->AddAction('preview', 'Preview', array(&$this, '_PreviewPage'), false);        
        $form->AddAction('cancel', 'Cancel', array(&$this, 'ManagePages_List'), false);
                
        // Form Fields
        $form->Add(new RichtextField(&$form, 'wysiwyg', 'Content', null, $page->n_body));
        $form->AddFieldSet('Summary');
        $form->Add(new TextField(&$form, 'title', 'Title', null, $page->n_title, true));        
        $form->Add(new TextField(&$form, 'author', 'Author', null, $page->_User->DataObject->username, true));
        $form->Add(new DateField(&$form, 'created', 'Created', null, $page->n_created, true));
        $form->Add(new TextareaField(&$form, 'teaser', 'Teaser', null, $page->n_teaser));        
        $form->AddFieldSet('Meta Data');
        $form->Add(new TextareaField(&$form, 'meta_keywords', 'Keywords', null, $page->n_meta_keywords));
        $form->Add(new TextareaField(&$form, 'meta_description', 'Description', null, $page->n_meta_description));
        $form->Add(new TextField(&$form, 'meta_robots', 'Robots', null, $page->n_meta_robots));
        
        // Process the form
        $form->Process();            
    }
    
    function ManagePages_Delete($selected = array())
    {
        $error   = array();
        $success = array();

        if (count($selected) > 0)
        {
	        for ($i = 0, $imax = count($selected); $i < $imax; $i++)
	        {
	            if ($this->_DAL->Delete('page', $selected[$i]) === false)
	                $error[] = $selected[$i];
	            else 
	                $success[] = $selected[$i];
	        }
	        
	        if (count($error) > 0)
	            $this->_Model->Set('Page.Messages.Error', array('Error Deleting Pages \''.implode(',', $error).'\''));
	            
	        if (count($success) > 0)
	            $this->_Model->Set('Page.Messages.Completed', array('Pages \''.implode(',', $success).'\' successfully deleted.'));
        }
        else 
        	$this->_Model->Set('Page.Messages.Information', array('One or more pages must be selected before clicking delete.'));

        	
        $this->ManageArticles_List();
    } 
   
    /*
     * Private Module Methods 
     */
    
    function _CreatePage()
    {           
        $this->_DAL->Insert('page', array('n_uid'              => 1,
                                          'n_title'            => $_POST['title'],
                                          'n_teaser'           => $_POST['teaser'],
                                          'n_body'             => $_POST['wysiwyg'],
                                          'n_meta_robots'      => $_POST['meta_robots'],
                                          'n_meta_description' => $_POST['meta_description'],
                                          'n_meta_keywords'    => $_POST['meta_keywords'])); 
		$this->ManagePages_List();
		$this->_Model->Set('Page.Messages.Completed', array('New Page Created.'));
    }   
    
    function _ModifyPage()
    {
    	/*
    	 * Grab a dataobject instance of the article to be modified
    	 */
    	
    	if ( !isset($_GET['pid']) || !is_numeric($_GET['pid']) || (($page = $this->_DAL->Select('page', $_GET['pid'])) === false))
    	{
    		$this->_Model->Set('Page.Messages.Error', array('Page \''.$_GET['pid'].'\' does not exist.'));
    		$this->ManageArticles_List();
    		return false;
    	}  
    	
    	/*
    	 * Modify the article
    	 */  	
    	
    	$page->n_uid              = 1;
    	$page->n_title            = $_POST['title'];
    	$page->n_teaser           = $_POST['teaser'];
    	$page->n_body             = $_POST['wysiwyg'];
    	$page->n_meta_robots      = $_POST['meta_robots'];
    	$page->n_meta_description = $_POST['meta_description'];
    	$page->n_meta_keywords    = $_POST['meta_keywords'];
    	
    	// Save Changes
    	$page->Save();
    	
    	$this->ManagePages_List();
    }  
    
    function _RecordSet_Page($data)
    {
    	return '<a href="http://'.$this->GetAction('page', 'modify').'?pid='.$data['id'].'">'.$data['n_title'].'</a>';
    }      
}

?>